package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.ProTeamUser;
import com.ruoyi.system.mapper.ProTeamMapper;
import com.ruoyi.system.domain.ProTeam;
import com.ruoyi.system.service.IProTeamService;

/**
 * 项目团队管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-07
 */
@Service
public class ProTeamServiceImpl implements IProTeamService 
{
    @Autowired
    private ProTeamMapper proTeamMapper;

    /**
     * 查询项目团队管理
     * 
     * @param teamId 项目团队管理主键
     * @return 项目团队管理
     */
    @Override
    public ProTeam selectProTeamByTeamId(Long teamId)
    {
        return proTeamMapper.selectProTeamByTeamId(teamId);
    }

    /**
     * 查询项目团队管理列表
     * 
     * @param proTeam 项目团队管理
     * @return 项目团队管理
     */
    @Override
    public List<ProTeam> selectProTeamList(ProTeam proTeam)
    {
        return proTeamMapper.selectProTeamList(proTeam);
    }

    /**
     * 新增项目团队管理
     * 
     * @param proTeam 项目团队管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProTeam(ProTeam proTeam)
    {
        int rows = proTeamMapper.insertProTeam(proTeam);
        insertProTeamUser(proTeam);
        return rows;
    }

    /**
     * 修改项目团队管理
     * 
     * @param proTeam 项目团队管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProTeam(ProTeam proTeam)
    {
        proTeamMapper.deleteProTeamUserByTeamId(proTeam.getTeamId());
        insertProTeamUser(proTeam);
        return proTeamMapper.updateProTeam(proTeam);
    }

    /**
     * 批量删除项目团队管理
     * 
     * @param teamIds 需要删除的项目团队管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProTeamByTeamIds(Long[] teamIds)
    {
        proTeamMapper.deleteProTeamUserByTeamIds(teamIds);
        return proTeamMapper.deleteProTeamByTeamIds(teamIds);
    }

    /**
     * 删除项目团队管理信息
     * 
     * @param teamId 项目团队管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProTeamByTeamId(Long teamId)
    {
        proTeamMapper.deleteProTeamUserByTeamId(teamId);
        return proTeamMapper.deleteProTeamByTeamId(teamId);
    }

    /**
     * 新增项目团队成员信息
     * 
     * @param proTeam 项目团队管理对象
     */
    public void insertProTeamUser(ProTeam proTeam)
    {
        List<ProTeamUser> proTeamUserList = proTeam.getProTeamUserList();
        Long teamId = proTeam.getTeamId();
        if (StringUtils.isNotNull(proTeamUserList))
        {
            List<ProTeamUser> list = new ArrayList<ProTeamUser>();
            for (ProTeamUser proTeamUser : proTeamUserList)
            {
                proTeamUser.setTeamId(teamId);
                list.add(proTeamUser);
            }
            if (list.size() > 0)
            {
                proTeamMapper.batchProTeamUser(list);
            }
        }
    }
}
